package com.vv.edu.info.manager.dao;

import com.vv.edu.info.manager.domain.StudentDomain;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    // 创建学生对象数组
    private static final ArrayList<StudentDomain> stus = new ArrayList<>();

    static{
        StudentDomain stu1 = new StudentDomain("001", "vv", "18", "1999-07-30");
        stus.add(stu1);
    }

    public boolean addStuent(StudentDomain stu) {
        stus.add(stu);
        return true;
    }

    public StudentDomain[] findAllStudent() {
        StudentDomain[] students = new StudentDomain[stus.size()];
        for (int i = 0; i < students.length; i++) {
            students[i] = stus.get(i);
        }
        return students;
    }

    public void deleteStudentById(String delId) {
        // 1. 查找id在容器中所在的索引位置
        int index = getIndex(delId);
        // 2. 将该索引位置删除
        stus.remove(index);
    }

    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            StudentDomain stu = stus.get(i);
            if(stu != null && stu.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updataStudent(String updataId, StudentDomain newStu) {
        // 1. 查找updataId 在容器中的索引位置
        int index = getIndex(updataId);
        // 2. 将对应索引查寻到的对象进行替换
        stus.set(index, newStu);
    }
}
